import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'
import './styles/element-variables.scss'

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log

import * as filters from './filters' // global filters

import axios from 'axios'
import VueAxios from 'vue-axios'

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
import { mockXHR } from '../mock'
if (process.env.NODE_ENV === 'production') {
  mockXHR()
}

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})
Vue.use(VueAxios, axios)
// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false

// 获取指定名称的cookie的值
Vue.prototype.myGetCookie = function(objName) {
  const arrStr = document.cookie.split(';')
  for (let i = 0; i < arrStr.length; i++) {
    const temp = arrStr[i].split('=')
    if (temp[0] === ' ' + objName) {
      return unescape(temp[1])
    }
  }
}
// 两个参数，一个是cookie的名子，一个是值,一个是时间
Vue.prototype.mySetCookie = function(name, value, duration) {
  const Days = duration // 此 cookie 将被保存 30 天
  const exp = new Date() // new Date("December 31, 9998");
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000)
  document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
}

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
